package com.weilus;

import com.weilus.clients.FluxService;
import com.weilus.clients.dtos.MQMessage;
import com.weilus.refresh.ConsumerConfigMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;

/**
 * Created by liutq on 2018/12/12.
 */

@Configuration
@RestController
public class ConsumerController {

    @Autowired
    FluxService fluxService;

    @Autowired
    ConsumerConfigMap config;

    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/{serviceId}")
    public Mono<List<ServiceInstance>> clients(@PathVariable("serviceId") String serviceId){
        return Mono.just(discoveryClient.getInstances(serviceId));
    }

    @GetMapping("test/sayHello")
    public Mono<String> sayHello(){
        return fluxService.sayHello(Collections.singletonMap("name", "jhon"));
    }

    @GetMapping("test/sayName")
    public String sayName(){
        return config.getName();
    }

    @GetMapping("test/hiMan")
    public Mono<String> hiMan(){
        return fluxService.hiMan(Collections.singletonMap("name", "jhon"));
    }

    @GetMapping("test/login")
    public Mono<String> login(Long delay,@RequestParam(value = "ex",defaultValue = "false") Boolean ex){
        MQMessage login = new MQMessage();
        login.setName("test-login");
        login.setAge("用户 test登录成功...测试记录日志");
        login.setDelay(delay);
        login.setEx(ex);
        MQMessage dayMoney = new MQMessage();
        dayMoney.setName("every-day-money");
        dayMoney.setAge("用户 test 记20分");
        dayMoney.setDelay(delay);
        dayMoney.setEx(ex);
        return
//                fluxService.hiMan1(Collections.singletonMap("ex", "jhon"))
//               .then(
                       Mono.zip(fluxService.loginLog(login), fluxService.everyDayMoney(dayMoney), (r1,r2)->r1+r2
//               )
        );
    }


}
